//
//  AFNetworking.m
//  zhijianshejian
//
//  Created by admin on 16/7/1.
//  Copyright © 2016年 haoniu-duhuan. All rights reserved.
//

#import "AFNetworking.h"

#import "Reachability.h"
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>

@implementation AFNetworking

#pragma mark 获得网络状态
+(NSString *)getNetWorkStates{
    UIApplication *app = [UIApplication sharedApplication];
    NSArray *children = [[[app valueForKeyPath:@"statusBar"]valueForKeyPath:@"foregroundView"]subviews];
    NSString *state = [[NSString alloc]init];
    int netType = 0;
    //获取到网络返回码
    for (id child in children) {
        //NSLog(@"=====child:%@",child);
        if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {

//            NSLog(@"getName:dataNetworkType=%@,",[child valueForKeyPath:@"dataNetworkType"]);
//            NSLog(@"getName:wifiStrengthRaw=%@,",[child valueForKeyPath:@"wifiStrengthRaw"]);
//            NSLog(@"getName:wifiStrengthBars=%@,",[child valueForKeyPath:@"wifiStrengthBars"]);
//            NSLog(@"getName:enableRSSI=%@,",[child valueForKeyPath:@"enableRSSI"]);
//            NSLog(@"getName:showRSSI=%@,",[child valueForKeyPath:@"showRSSI"]);
            
            //获取到状态栏
            netType = [[child valueForKeyPath:@"dataNetworkType"]intValue];
            NSLog(@"=====dataNetworkType:%d",netType);
            switch (netType) {
                case 0:
                    state = @"无网络";
                    //无网模式
                    break;
                case 1:
                    state = @"2G";
                    break;
                case 2:
                    state = @"3G";
                    break;
                case 3:
                    state = @"4G";
                    break;
                case 5:
                {
                    state = @"WIFI";
                }
                    break;
                default:
                    break;
            }
        }else if ([child isKindOfClass:NSClassFromString(@"UIStatusBarServiceItemView")]) {
            id type = [child valueForKeyPath:@"serviceString"];
            NSLog(@"serviceString:%@", type);
        }
    }
    //根据状态选择
    return state;
}

#pragma mark 检测网路状态
+ (void)netWorkStatus
{
    /**
     AFNetworkReachabilityStatusUnknown          = -1,  // 未知
     AFNetworkReachabilityStatusNotReachable     = 0,   // 无连接
     AFNetworkReachabilityStatusReachableViaWWAN = 1,   // 3G 花钱
     AFNetworkReachabilityStatusReachableViaWiFi = 2,   // WiFi
     */
    // 如果要检测网络状态的变化,必须用检测管理器的单例的startMonitoring
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    
    // 检测网络连接的单例,网络变化时的回调方法
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        NSLog(@"%ld", (long)status);
        switch ((long)status) {
            case 2:
            {
                NSLog(@"WIFI");
            }
                break;
            case 1:
            {
                NSLog(@"流量");
            }
                break;
            case 0:
            {
                NSLog(@"未连接");
            }
                break;
            default:
            {
                NSLog(@"未知错误");
            }
                break;
        }
    }];
}


+ (NSString *)getNetconnType{
    
    NSString *netconnType = @"";
    
    Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
    
    switch ([reach currentReachabilityStatus]) {
        case NotReachable:// 没有网络
        {
            
            netconnType = @"无网络";
        }
            break;
            
        case ReachableViaWiFi:// Wifi
        {
            netconnType = @"WIFI";
        }
            break;
            
        case ReachableViaWWAN:// 手机自带网络
        {
            // 获取手机网络类型
            CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
            
            NSString *currentStatus = info.currentRadioAccessTechnology;
            
            if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyGPRS"]) {
                
                netconnType = @"2.5G GPRS";
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyEdge"]) {
                
                netconnType = @"2.75G EDGE";
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyWCDMA"]){
                
                netconnType = @"3G";
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyHSDPA"]){
                
                netconnType = @"3.5G HSDPA";
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyHSUPA"]){
                
                netconnType = @"3.5G HSUPA";
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMA1x"]){
                
                netconnType = @"2G";
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORev0"]){
                
                netconnType = @"3G";
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORevA"]){
                
                netconnType = @"3G";
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORevB"]){
                
                netconnType = @"3G";
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyeHRPD"]){
                
                netconnType = @"HRPD";
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyLTE"]){
                
                netconnType = @"4G";
            }
        }
            break;
            
        default:
            break;
    }
    
    return netconnType;
}


@end
